var savepath="/data/local/tmp/";
var savepath="/sdcard/";
function savedex(dexptr,savepath) {

    try {
         var dexfilebegin = ptr(dexptr).add(Process.pointerSize*1).readPointer(); // 16进制
         var dexfilesize = ptr(dexptr).add(Process.pointerSize*2).readU32(); // 16进制
        var dex = new File(savepath+"_"+dexfilesize+".dex","a");
        if (dex!=null){
            var content= ptr(dexfilebegin).readByteArray(dexfilesize);
            dex.write(content)
            dex.flush();
            dex.close();
            console.log("dumdex:\t"+savepath+"_"+dexfilesize+".dex")
        }



    }catch (e) {
        console.warn("dumdex:\t"+e);
    }
}

function getClassloaders() {
    Java.perform(function () {
        Java.choose("dalvik.system.DexFile",{
            onMatch: function (dexfile) {
                // 取出 mCookie
                var mCookie = dexfile.mCookie.value;
            //    打印类型
            //      console.log(mCookie.$className); // [J
            // 遍历
                var Array=Java.use("java.lang.reflect.Array");
                //获取数组的长度
                var size= Array.getLength(mCookie);
                var i=0;
                //遍历
                for (i=0;i<size;i++){
                    try {

                    var longvalue=Array.getLong(mCookie,i);
                    var dexptr=ptr(Array.getLong(mCookie,i)+"");
                    if (longvalue == 0){
                        break
                    }
                    // 获取mCookie数组中的每个元素
                    console.log(i+"-->"+longvalue+":\t"+dexptr+"\tprocess:"+Process.pointerSize);
                    savedex(dexptr,savepath)
                    // console.log(i+"-2->"+dexfilebegin+""+dexfilesize)
                    }catch (e) {
                        console.error("error"+e);
                        continue
                    }

                }
            },onComplete: function () {
                console.warn("exit");

            }
        })
    });
}


setTimeout(getClassloaders)

// frida -U -F -l getClassloaders
// frida -H 127.0.0.1:8888 -f com.google.android.apps.maps  -l getClassloaders.js --no-pause
// frida -H 127.0.0.1:8888 -f com.qiyi.video  -l getClassloaders.js --no-pause


// frida -U -f com.qiyi.video  -l getClassloaders.js --no-pause

